﻿using System;
using System.Collections.Generic;
using System.Drawing; 
using System.Linq;
using System.Text;
using System.IO;
using System.Web;
using System.Drawing.Imaging;


namespace YTBZ.PUB.COMMON
{
    public class HandleFile
    {
        /// <summary>
        /// 获取磁盘路径
        /// </summary>
        /// <param name="netPath">虚拟路径</param>
        /// <returns></returns>
        public static String GetDiskPath(string netPath)
        {
            return HttpContext.Current.Server.MapPath(netPath);
        }

        /// <summary>
        /// 获取文件路径
        /// </summary>
        /// <param name="fn"></param>
        /// <returns></returns>
        public static string GetFilePath(string fn)
        {
            int i = fn.LastIndexOf('/');
            if (i < 0)
                i = fn.LastIndexOf('\\');
            if (i < 0) return fn;
            return fn.Substring(0, i + 1);
        }

        /// <summary>
        /// 创建指定文件夹
        /// </summary>
        /// <param name="Paths">虚拟路径</param>
        /// <returns></returns>
        public static bool CreateFolders(String Paths)
        {
            try
            {
                Paths = GetDiskPath(Paths);
                if (Directory.Exists(Paths) == false)
                    Directory.CreateDirectory(Paths);
                return true;
            }
            catch (Exception e)
            {

                return false;
            }
        }

        /// <summary>
        /// 根据磁盘路径创建文件夹
        /// </summary>
        /// <param name="Paths">磁盘路径</param>
        /// <returns></returns>
        public static bool CreateDiskFolders(String Paths)
        {
            try
            {

                if (Directory.Exists(Paths) == false)
                    Directory.CreateDirectory(Paths);
                return true;
            }
            catch (Exception e)
            {

                return false;
            }
        }

        /// <summary>
        /// 连接路径和文件名，同时创建路径
        /// </summary>
        /// <param name="netPath"></param>
        /// <param name="strName"></param>
        /// <returns></returns>
        public static String PathCatFile(string netPath, string strName)
        {
            try
            {
                String tf = netPath.Replace("//", "/");

                if (!CreateFolders(netPath)) return "";

                string fn = string.Format("{0}{1}", tf[tf.Length - 1] == '/' ? tf : tf + "/", strName);
                return fn;
            }
            catch (Exception e)
            {
                return "";

            }  
        }
       

        /// <summary>
        /// 合并文件路径和文件名 如果文件夹不存，同时创建文件夹
        /// </summary>
        /// <param name="netPath">虚拟路径</param>
        /// <param name="strName">文件名</param>
        /// <returns></returns>
        public static String GetFileName(string netPath, string strName)
        {
            try
            {
                String tf = GetDiskPath(netPath);

                if (!CreateFolders(netPath)) return "";

                string fn = string.Format("{0}{1}", tf[tf.Length - 1] == '\\' ? tf : tf + "\\", strName);
                return fn;
            }
            catch (Exception e)
            {
                return "";

            }  
        }

        public static string GetFileExt(string FileName)
        {
            if (string.IsNullOrEmpty(FileName))
                return "";
            string ext = Path.GetExtension(FileName);
            return ext;
        }


        public static bool Exists(string netPath)
        {
            if (File.Exists(GetDiskPath(netPath)))
                return true;
            return false;
        }

        public static bool DeleteFile(string netPage)
        {
            string rP = GetDiskPath(netPage);
            if (File.Exists(rP))
            {
                File.Delete(rP);
                return true;
            }
            return false;
        }

        public static bool IsAllowExt(String fnExt, string extList)
        {
            fnExt = fnExt.ToLower().Trim();
            string[] exts = extList.Split('|');
            foreach (string a in exts)
            {
                if (a != null && a != "")
                {
                    if (a.ToLower().Trim() == fnExt)
                        return true;
                }
            }
            return false;
        }

        /// <summary>
        /// 按长和宽缩小图片
        /// </summary>
        /// <param name="file"></param>
        /// <param name="path"></param>
        /// <param name="iheight"></param>
        /// <param name="iwidth"></param>
        public static void CreateSmallestImage(HttpPostedFile file, string path, int iheight, int iwidth)
        {
            Bitmap bigBitmap = (Bitmap)System.Drawing.Image.FromStream(file.InputStream);
            int height = bigBitmap.Height;
            int width = bigBitmap.Width; //老的图片
            Rectangle srcRC = new Rectangle(0, 0, width, height);
            int newWidth, newHeight;
            if (height < iheight && width < iwidth)
            {
                newWidth = width;
                newHeight = height;

            }
            else if (width > height)
            {
                float bili = (float)iwidth / width;
                newWidth = iwidth;
                newHeight = Convert.ToInt32(bili * height);
            }
            else
            {
                float bili = (float)iheight / height;
                newHeight = iheight;
                newWidth = Convert.ToInt32(bili * width);
            }
            //Rectangle destRC = new Rectangle(iwidth / 2 - newWidth / 2, iheight / 2 - newHeight / 2, newWidth, newHeight);
            Size mysize = new Size(newWidth, newHeight);
            System.Drawing.Image newBitmap = new System.Drawing.Bitmap(mysize.Width, mysize.Height);

            Graphics g = Graphics.FromImage(newBitmap); //新的图片

            Rectangle destRC = new Rectangle(0, 0, newWidth, newHeight);
            //设置高质量插值法 
            g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
            //设置高质量,低速度呈现平滑程度 
            g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
            g.Clear(Color.White);
            g.DrawImage(bigBitmap, destRC, srcRC, GraphicsUnit.Pixel); //将老的压缩到新的里
            ImageFormat format = ImageFormat.Jpeg;
            newBitmap.Save(path, format);//保存
            g.Dispose();
            newBitmap.Dispose();
            bigBitmap.Dispose();
        }


    }
}
